<?php


namespace App\Api\Phalapialiyunsms;
use PhalApi\Api;
use App\Domain\Phalapialiyunsms\Main as SmsDomain;

use App\Domain\User\User as UserDomain;
use App\Domain\User\UserSession as UserSessionDomain;
use PhalApi\Exception\BadRequestException;


/**
 * 发短信接口
 * @desc 发短信接口
 */
class Sms extends Api
{



    public function getRules()
    {
        return array(
            'sendSMS' => array(
                'username' => array('name' => 'username', 'type' => 'string', 'require' => true, 'desc' => '用户名(手机号)'),
            ),
            'reSMS' => array(
                'username' => array('name' => 'username', 'type' => 'string', 'require' => true, 'desc' => '用户名(手机号)'),
            ),
            'addphoneSMS' => array(
                'mobile' => array('name' => 'mobile', 'type' => 'string', 'require' => true, 'desc' => '用户名(手机号)'),
            ),
        );  
    }


    function generateRandomString() {
        $randomString = '';
        for ($i = 0; $i < 4; $i++) {
            $randomNumber = rand(0, 9);
            $randomString .= $randomNumber;
        }
        return $randomString;
    }


    /**
     * 添加任务手机
     * @desc 发送短信
     * @param string $phone 手机号
     * @param string $type 短信类型
     * @param string $SignName 签名名称
     * @param string $TemplateCode 模版CODE
     * @param string $TemplateParam 模版参数
     * @return string
     */
    public function addphoneSMS()
    {

       
        // $phone, $type, $SignName, $TemplateCode, $TemplateParam
        $sms = new SmsDomain();
        // 调用发短信接口
        $type = 'sms_TemplateCode_register'; //短信模版类型
        if(!$this->mobile){
            throw new BadRequestException('手机号不能为空');
        }

        
        //判断手机号是否已存在
        $phone = \PhalApi\DI()->notorm->phone_list->where(array('mobile'=>$this->mobile ))->fetchOne();

        if( $phone ){
            throw new BadRequestException('该任务号已存在');
        }
        

        $phone = $this->mobile;            // 手机号
        $SignName = '可抖云';            // 签名名称
        $TemplateCode = 'SMS_98345315';   // 模版CODE(用户注册)
        $smsCode = $this->generateRandomString(); //随机生成6位短信验证码
        $TemplateParam = array("code" => $smsCode);
        $sms = new SmsDomain();
        // 调用发短信接口
        $data = $sms->aliyunSendSms($phone, $SignName, $TemplateCode, $TemplateParam);
        // {
        // "Message": "OK",
        // "RequestId": "21C9C21E-83C2-4223-955D-F08F008B2480",
        // "BizId": "652710985911586460^0",
        // "Code": "OK"
        // }

        if ($data['Code'] === 'OK') {
            // 设置
            \PhalApi\DI()->cache->set($phone, $smsCode, 600);
            return "发送成功";
            // {
            // "code": 200,
            // "msg": "发送成功",
            // "data": 893363
            // }
        } else {
            // return array('code' => 201, 'msg' => $data['Message'], 'data' => $data);
            throw new BadRequestException($data['Message'], 1);
        }
     

    }



    /**
     * 改密码短信
     * @desc 发送短信
     * @param string $phone 手机号
     * @param string $type 短信类型
     * @param string $SignName 签名名称
     * @param string $TemplateCode 模版CODE
     * @param string $TemplateParam 模版参数
     * @return string
     */
    public function reSMS()
    {
        // $phone, $type, $SignName, $TemplateCode, $TemplateParam
        $sms = new SmsDomain();
        // 调用发短信接口
        $type = 'sms_TemplateCode_register'; //短信模版类型
        if(!$this->username){
            return '手机号不能为空';
        }

        $domain = new UserDomain();
        $user = $domain->getUserByUsername($this->username, 'id');
        if ($user) {

            $phone = $this->username;            // 手机号
            $SignName = '可抖云';            // 签名名称
            $TemplateCode = 'SMS_98345315';   // 模版CODE(用户注册)
            $smsCode = $this->generateRandomString(); //随机生成6位短信验证码
            $TemplateParam = array("code" => $smsCode);
            $sms = new SmsDomain();
            // 调用发短信接口
            $data = $sms->aliyunSendSms($phone, $SignName, $TemplateCode, $TemplateParam);
            // {
            // "Message": "OK",
            // "RequestId": "21C9C21E-83C2-4223-955D-F08F008B2480",
            // "BizId": "652710985911586460^0",
            // "Code": "OK"
            // }
    
            if ($data['Code'] === 'OK') {
                // 设置
                \PhalApi\DI()->cache->set($phone, $smsCode, 600);
                return "发送成功";
                // {
                // "code": 200,
                // "msg": "发送成功",
                // "data": 893363
                // }
            } else {
                // return array('code' => 201, 'msg' => $data['Message'], 'data' => $data);
                throw new BadRequestException($data['Message'], 1);
            }
        }else{
            throw new BadRequestException('帐号不存在');
        }

    }



    /**
     * 发送短信
     * @desc 发送短信
     * @param string $phone 手机号
     * @param string $type 短信类型
     * @param string $SignName 签名名称
     * @param string $TemplateCode 模版CODE
     * @param string $TemplateParam 模版参数
     * @return string
     */
    public function sendSMS()
    {
        // $phone, $type, $SignName, $TemplateCode, $TemplateParam
        $sms = new SmsDomain();
        // 调用发短信接口
        $type = 'sms_TemplateCode_register'; //短信模版类型
        if(!$this->username){
            return '手机号不能为空';
        }

        $domain = new UserDomain();
        $user = $domain->getUserByUsername($this->username, 'id');
        if ($user) {
            throw new BadRequestException($this->username . ' 账号已注册, 请直接登录');
        }

        $phone = $this->username;            // 手机号
        $SignName = '可抖云';            // 签名名称
        $TemplateCode = 'SMS_98345315';   // 模版CODE(用户注册)
        $smsCode = $this->generateRandomString(); //随机生成6位短信验证码
        $TemplateParam = array("code" => $smsCode);
        $sms = new SmsDomain();
        // 调用发短信接口
        $data = $sms->aliyunSendSms($phone, $SignName, $TemplateCode, $TemplateParam);
        // {
        // "Message": "OK",
        // "RequestId": "21C9C21E-83C2-4223-955D-F08F008B2480",
        // "BizId": "652710985911586460^0",
        // "Code": "OK"
        // }

        if ($data['Code'] === 'OK') {
            // 设置
            \PhalApi\DI()->cache->set($phone, $smsCode, 600);
            return "发送成功";
            // {
            // "code": 200,
            // "msg": "发送成功",
            // "data": 893363
            // }
        } else {
            // return array('code' => 201, 'msg' => $data['Message'], 'data' => $data);
            throw new BadRequestException($data['Message'], 1);
        }

    }



}




?>